const arrUnitSecond = [60 * 60 * 24 * 365, 60 * 60 * 24, 60 * 60, 60, 1];
const arrUnitText = ['年', '天', '时', '分', '秒'];

export const EvalTool = {
	tadd: (time, value, unit = 's') => M(time).add(value, unit).format(),
	tiff: (timeA, timeB, unit = 's') => M(timeA).diff(M(timeB), unit),
	tiffText: (timeA, timeB, range = 2) => {
		const ma = M(timeA);
		const mb = M(timeB);

		let diff = Math.abs(ma.diff(mb, 's', true));
		let result = '';

		let rangeUnit = range;
		for(let i = 0; i < arrUnitText.length; i++) {
			const threshold = arrUnitSecond[i];

			let count = 0;
			while(diff > threshold) {
				diff -= threshold;
				count++;
			}

			if(count) {
				result += `${count}${arrUnitText[i]}`;

				if(!--rangeUnit) {
					break;
				}
			}
		}

		return result || '0秒！！！！';
	},
	Q: {
		regen(old, max, increment, interval, tnow, timeBase) {
			const timeElapsed = EvalTool.tiff(tnow, timeBase);
			const tick = ~~(timeElapsed / interval);

			const apTick = old + tick * increment;

			const now = (apTick > max) ? max : apTick;
			const leak = apTick - max;

			const timeElapsedMax = (max - old) / increment * interval;
			const tiffFull = EvalTool.tiffText(tnow, EvalTool.tadd(timeBase, timeElapsedMax), 2);

			return { now, tiffFull, leak };
		}
	}
};
